<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="工序类别" prop="proceCategoryTypeName">
                <el-input v-model="queryParams.proceCategoryTypeName" placeholder="请输入工序类别" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="工价系数" prop="wageCoeffic">
                <el-input v-model="queryParams.wageCoeffic" placeholder="请输入工价系数" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="单位工价" prop="unitOfWages">
                <el-input v-model="queryParams.unitOfWages" placeholder="请输入单位工价" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['craft:coeffic:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['craft:coeffic:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['craft:coeffic:remove']">删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="coefficList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" type="index" align="center" />
            <el-table-column label="工序类别" align="center" prop="proceCategoryTypeName" width="120" />
            <el-table-column label="工价系数" align="center" prop="wageCoeffic" />
            <el-table-column label="单位工价" align="center" prop="unitOfWages" />
            <el-table-column label="粗加工" align="center">
                <el-table-column label="系数" align="center" prop="roughingCoeffic" />
                <el-table-column label="单价(¥)" align="center" prop="roughingPrice" />
            </el-table-column>
            <el-table-column label="半精加工系数" align="center">
                <el-table-column label="系数" align="center" prop="semifinishingCoeffic" />
                <el-table-column label="单价(¥)" align="center" prop="semifinishingPrice" />
            </el-table-column>
            <el-table-column label="精加工" align="center">
                <el-table-column label="系数" align="center" prop="finishingCoeffic" />
                <el-table-column label="单价(¥)" align="center" prop="finishingPrice" />
            </el-table-column>
            <el-table-column label="高精加工" align="center">
                <el-table-column label="系数" align="center" prop="highFinishingCoeffic" />
                <el-table-column label="单价(¥)" align="center" prop="highFinishingPrice" />
            </el-table-column>
            <el-table-column label="创建人ID" align="center" prop="createUserId" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="120">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作时间" align="center" prop="opTime" width="120">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.opTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作人ID" align="center" prop="opUserId" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['craft:coeffic:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['craft:coeffic:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改工序工价系数对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="110px">
                <el-form-item label="工序类别" prop="proceCategoryTypeId">
                    <el-select v-model="form.proceCategoryTypeId" :value="form.proceCategoryTypeId" placeholder="请选择工序类别" style="width: 100%" @change="handleSelectChange">
                        <el-option-group v-for="group in gropCategory" :key="group.label" :label="group.label">
                            <el-option v-for="item in group.options" :key="item.value" :label="item.label" :value="item.value">
                            </el-option>
                        </el-option-group>
                    </el-select>
                </el-form-item>
                <el-form-item label="工价系数" prop="wageCoeffic">
                    <el-input-number v-model="form.wageCoeffic" placeholder="请输入工价系数" :precision="2" :step="0.01" @change="calculate" />
                </el-form-item>
                <el-form-item label="单位工价" prop="unitOfWages">
                    <el-input-number v-model="form.unitOfWages" placeholder="请输入单位工价" :disabled="true" :precision="2" :controls="false" />
                </el-form-item>
                <el-form-item label="排序" prop="orderBy">
                    <el-input v-model="form.orderBy" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="粗加工系数" prop="roughingCoeffic">
                    <el-input-number v-model="form.roughingCoeffic" placeholder="请输入粗加工系数" :precision="2" :step="0.01" @change="handleCoefficCalculate('roughing')" />
                </el-form-item>
                <el-form-item label="粗加工单价" prop="roughingPrice">
                    <el-input-number v-model="form.roughingPrice" placeholder="请输入粗加工单价" :disabled="true" :precision="2" :controls="false" />
                </el-form-item>
                <el-form-item label="半精加工系数" prop="semifinishingCoeffic">
                    <el-input-number v-model="form.semifinishingCoeffic" placeholder="请输入半精加工系数" :precision="2" :step="0.01" @change="handleCoefficCalculate('semifinishing')" />
                </el-form-item>
                <el-form-item label="半精加工单价" prop="semifinishingPrice">
                    <el-input-number v-model="form.semifinishingPrice" placeholder="请输入半精加工单价" :disabled="true" :precision="2" :controls="false" />
                </el-form-item>
                <el-form-item label="精加工系数" prop="finishingCoeffic">
                    <el-input-number v-model="form.finishingCoeffic" placeholder="请输入精加工系数" :precision="2" :step="0.01" @change="handleCoefficCalculate('finishing')" />
                </el-form-item>
                <el-form-item label="精加工单价" prop="finishingPrice">
                    <el-input-number v-model="form.finishingPrice" placeholder="请输入精加工单价" :disabled="true" :precision="2" :controls="false" />
                </el-form-item>
                <el-form-item label="高精加工系数" prop="highFinishingCoeffic">
                    <el-input-number v-model="form.highFinishingCoeffic" placeholder="请输入高精加工系数" :precision="2" :step="0.01" @change="handleCoefficCalculate('highFinishing')" />
                </el-form-item>
                <el-form-item label="高精加工单价" prop="highFinishingPrice">
                    <el-input-number v-model="form.highFinishingPrice" placeholder="请输入高精加工单价" :disabled="true" :precision="2" :controls="false" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listCoeffic,
    getCoeffic,
    delCoeffic,
    addCoeffic,
    updateCoeffic,
    initCoefficData,
} from "@/api/craft/coeffic";

export default {
    name: "Coeffic",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 工序工价系数表格数据
            coefficList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                proceCategoryTypeName: null,
                wageCoeffic: null,
                unitOfWages: null,
            },
            // 表单参数
            form: {
                id: null,
                proceCategoryTypeName: null,
                proceCategoryTypeId: null,
                wageCoeffic: null,
                unitOfWages: null,
                roughingCoeffic: null,
                roughingPrice: null,
                semifinishingCoeffic: null,
                semifinishingPrice: null,
                finishingCoeffic: null,
                finishingPrice: null,
                highFinishingCoeffic: null,
                highFinishingPrice: null,
                createTime: null,
                createUserId: null,
                opTime: null,
                opUserId: null,
                orderBy: null,
            },
            // 表单校验
            rules: {
                proceCategoryTypeId: [
                    {
                        required: true,
                        message: "工序类别不能为空",
                        trigger: "blur",
                    },
                ],
                wageCoeffic: [
                    {
                        required: true,
                        message: "工价系数不能为空",
                        trigger: "blur",
                    },
                ],
                unitOfWages: [
                    {
                        required: true,
                        message: "单位工价不能为空",
                        trigger: "blur",
                    },
                ],
                orderBy: [
                    {
                        required: true,
                        message: "排序不能为空",
                        trigger: "blur",
                    },
                ],
                roughingCoeffic: [
                    {
                        required: true,
                        message: "粗加工系数不能为空",
                        trigger: "blur",
                    },
                ],
                roughingPrice: [
                    {
                        required: true,
                        message: "粗加工单价不能为空",
                        trigger: "blur",
                    },
                ],
                semifinishingCoeffic: [
                    {
                        required: true,
                        message: "半精加工系数不能为空",
                        trigger: "blur",
                    },
                ],
                semifinishingPrice: [
                    {
                        required: true,
                        message: "半精加工单价不能为空",
                        trigger: "blur",
                    },
                ],
                finishingCoeffic: [
                    {
                        required: true,
                        message: "精加工系数不能为空",
                        trigger: "blur",
                    },
                ],
                finishingPrice: [
                    {
                        required: true,
                        message: "精加工单价不能为空",
                        trigger: "blur",
                    },
                ],
                highFinishingCoeffic: [
                    {
                        required: true,
                        message: "高精加工系数不能为空",
                        trigger: "blur",
                    },
                ],
                highFinishingPrice: [
                    {
                        required: true,
                        message: "高精加工单价不能为空",
                        trigger: "blur",
                    },
                ],
            },
            gropCategory: [],
            standardProcessPrice: null,
            processOrderBy: null,
        };
    },
    created() {
        this.getList();
        this.initData();
    },
    methods: {
        initData() {
            initCoefficData().then((response) => {
                console.log(response.data);
                this.gropCategory = response.data.categoryList;
                this.standardProcessPrice = response.data.standardProcessPrice;
                this.processOrderBy = response.data.processOrderBy;
            });
        },
        /** 查询工序工价系数列表 */
        getList() {
            this.loading = true;
            listCoeffic(this.queryParams).then((response) => {
                this.coefficList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                proceCategoryTypeName: null,
                proceCategoryTypeId: null,
                wageCoeffic: null,
                unitOfWages: null,
                roughingCoeffic: null,
                roughingPrice: null,
                semifinishingCoeffic: null,
                semifinishingPrice: null,
                finishingCoeffic: null,
                finishingPrice: null,
                highFinishingCoeffic: null,
                highFinishingPrice: null,
                orderBy: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            console.log()
            if (this.gropCategory.length==0) {
                this.$message({
                    message: "请新增工序类别",
                    type: "warning",
                });
            } else {
                this.reset();
                this.open = true;
                this.title = "添加工序工价系数";
                this.form.orderBy = this.processOrderBy;
            }
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getCoeffic(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改工序工价系数";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateCoeffic(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addCoeffic(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                    this.initData();
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm(
                    '是否确认删除工序工价系数编号为"' + ids + '"的数据项？'
                )
                .then(function () {
                    return delCoeffic(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        handleSelectChange(v){
            let name = null;
            this.gropCategory.forEach(o=>{
                o.options.forEach(o1=>{
                    if(o1.value == v){
                        name = o1.label;
                    }
                })
            })
            this.form.proceCategoryTypeName = name;
        },
        /** 计算加工系数单价 */
        calculate(currentValue) {
            this.form.unitOfWages = currentValue * this.standardProcessPrice;
            this.form.roughingPrice =
                this.form.roughingCoeffic * this.form.unitOfWages;
            this.form.semifinishingPrice =
                this.form.semifinishingCoeffic * this.form.unitOfWages;
            this.form.finishingPrice =
                this.form.finishingCoeffic * this.form.unitOfWages;
            this.form.highFinishingPrice =
                this.form.highFinishingCoeffic * this.form.unitOfWages;
        },
        handleCoefficCalculate(type) {
            if (type === "roughing") {
                this.form.roughingPrice =
                    this.form.roughingCoeffic * this.form.unitOfWages;
            }
            if (type === "semifinishing") {
                this.form.semifinishingPrice =
                    this.form.semifinishingCoeffic * this.form.unitOfWages;
            }
            if (type === "finishing") {
                this.form.finishingPrice =
                    this.form.finishingCoeffic * this.form.unitOfWages;
            }
            if (type === "highFinishing") {
                this.form.highFinishingPrice =
                    this.form.highFinishingCoeffic * this.form.unitOfWages;
            }
        },
    },
};
</script>
